using Hospital.IService;
using Hospital.Model.entity;
using Hospital.Utility._MD5;
using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Mvc;

namespace Hospital.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PatientInfoController(IPatientInfoService patientInfoService) : ControllerBase
    {
        private readonly IPatientInfoService _patientInfoService = patientInfoService;
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [HttpPost("Login")]
        public async Task<ActionResult<ApiResult>> Login(string phone, string password)
        {
            password = MD5Helper.MD5Encrypt32(password);
            var patientInfo = await _patientInfoService.FindAsync(c => c.Phone == phone && c.Password == password);
            if (patientInfo == null)
            {
                return ApiResultHelper.Error(401, "用户名或密码错误");
            }
            return ApiResultHelper.Success(patientInfo);
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="patientInfo">患者信息</param>
        /// <returns></returns>
        [HttpPost("Register")]
        public async Task<ActionResult<ApiResult>> Register([FromBody] PatientInfo patientInfo)
        {
            var isFind = await _patientInfoService.FindAsync(c => c.Phone == patientInfo.Phone);
            if (isFind != null)
            {
                return ApiResultHelper.Error(409, "手机号已存在");
            }
            patientInfo.Password = MD5Helper.MD5Encrypt32(patientInfo.Password);
            await _patientInfoService.CreateAsync(patientInfo);
            return ApiResultHelper.Success("注册成功");
        }
    }
}